<?php
session_start();
if(!isset($_SESSION['username'])){
	header('Location: index.php');
}
$location = "main.php";
if(isset($_POST['submit'])){
	if($_POST['submit'] == "Delete"){
		$producer_id = $_POST['producer_id'];
		$model = $_POST['model'];
		$price = $_POST['money'];
		$dbConnection = mysqli_connect('localhost', 'root', 'root', 'tractor');
		$query = "delete from `producer` where `producer_id` = '$producer_id'";
		echo $query .' '. $query2;
		if (!mysqli_query($dbConnection, $query)){
			$error = mysqli_error($dbConnection);
			$_SESSION['message'] = 'error: ' . $error;
		}
		$location = "main.php";
	} else if($_POST['submit'] == "Buy"){
		if($_SESSION['money'] > $_POST['money']){
			$_SESSION['money'] -= $_POST['money'];
			$producer_id = $_POST['producer_id'];
			$model = $_POST['model'];
			$price = $_POST['money'];
			$money = $_SESSION['money'];
			$user = $_SESSION['username'];
			$queryMoney = "update user set `money` = '$money' where `user_name` = '$user'";
			$dbConnection = mysqli_connect('localhost', 'root', 'root', 'tractor');
			$query = "delete from `producer` where producer_id = '$producer_id'";
			if (!mysqli_query($dbConnection, $queryMoney)){
				$error = mysqli_error($dbConnection);
				$_SESSION['message'] = 'error: ' . $error;
			}
			if (!mysqli_query($dbConnection, $query)){
				$error = mysqli_error($dbConnection);
				$_SESSION['message'] = 'error: ' . $error;
			}
			$location = "main.php";
		} else{
			$_SESSION['message'] = "not enough money";
			$location = "main.php";
		}
	} else if($_POST['submit'] == "Edit"){
		$location = "edit_tractor.php?producer_id=" . $_POST['producer_id'];
	}
}

header("Location: $location");